package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.entity.Flower;
import com.ktjiaoyu.mapper.FlowerMapper;
import com.ktjiaoyu.service.FlowerService;
import com.ktjiaoyu.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("flowerService")
public class FlowerServiceImpl implements FlowerService {
    Logger logger= LoggerFactory.getLogger(FlowerServiceImpl.class);
    @Autowired
    private FlowerMapper flowerMapper;

    @Override
    public Page<Flower> findPage(Flower flower, Integer pageNo, Integer pageSize) {
        Page<Flower> page=new Page<>();
        //设置当前的页码数
        page.setPageNo(pageNo);
        //设置每页的条数
        page.setPageSize(pageSize);
        // 查询总数，计算总页数
        Integer count=flowerMapper.count(flower);
        page.setTotalCount(count);
        // 查询分页数据
        Integer pageBegin=(pageNo-1)*pageSize;
        List<Flower> data=flowerMapper.getPage(flower,pageBegin,pageSize);
        page.setData(data);
        return page;
    }

    @Override
    public boolean save(Flower flower) {
        return flowerMapper.insert(flower)>0;
    }

    @Override
    public Flower findById(Integer id) {
        return flowerMapper.getById(id);
    }
    @Override
    @Transactional
    public boolean update(Flower flower) {
        return flowerMapper.update(flower)>0;
    }

    @Override
    public boolean delete(Integer flowerId) {
        return flowerMapper.delete(flowerId)>0;
    }

    @Override
    public Flower findByAccount(String flowerName) throws Exception {
        try {
            return flowerMapper.getByAccount(flowerName);
        }catch (Exception e){
            logger.error("FlowerServiceImpl中的findByAccount方法执行发生错误。");
            throw e;
        }
    }
}
